package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.ComplainType;
import com.originskyseed.platform.cms.service.IComplainTypeService;
import com.originskyseed.platform.cms.vo.request.ComplainTypeRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 投诉类型表Controller类（OSI_COMPLAIN_TYPE）
 *
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/complainType")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class ComplainTypeController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ComplainTypeController.class);

	@DubboReference
	private IComplainTypeService complainTypeService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainTypeList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ComplainType>> complainTypeList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ComplainType>> result = null;
		try {
			// 请求对象
			ComplainTypeRequestVO requestVO = this.getParameterBean(ComplainTypeRequestVO.class);
			// 分页查询
			BasePage<ComplainType> page = new BasePage<ComplainType>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.complainTypeService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<ComplainType>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ComplainType>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainTypeSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<ComplainType> complainTypeSave(HttpServletRequest request) {
		BaseResponseBody<ComplainType> result = null;
		try {
			// 请求对象
			ComplainType complainType = this.getParameterBean(ComplainType.class);

			if (StringUtils.isNotBlank(complainType.getId())) {
				this.complainTypeService.updateByPrimaryKeySelective(complainType);
			} else {
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				complainType.setCreatorId(userInfo.getId());
				complainType.setCreator(userInfo.getName());
				complainType.setIsTop(0);
				Integer prority = this.complainTypeService.selectPrority();
				if (prority == null) {
					complainType.setPriority(0);
				} else {
					complainType.setPriority(prority + 1);
				}
				this.complainTypeService.insertSelective(complainType);
			}
			result = new BaseResponseBody<ComplainType>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), complainType);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ComplainType>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainTypeDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> complainTypeDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String ids = this.getParameterString("ids", null);

			this.complainTypeService.deleteByPrimaryKeys(Arrays.asList(ids.split(",")));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/complainTypeDetails.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<ComplainType> complainTypeDetails(HttpServletRequest request) {
		BaseResponseBody<ComplainType> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			ComplainType complainType = this.complainTypeService.selectByPrimaryKey(id);
			result = new BaseResponseBody<ComplainType>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), complainType);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ComplainType>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 修改置顶信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateIsTop.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody updateIsTop(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			ComplainType complainType = new ComplainType();
			complainType.setId(id);
			complainType.setIsTop(1);
			complainType.setUpdateTime(new Date());
			this.complainTypeService.updateIsTop(complainType);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 升序降序
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUpOrDown.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id", "upOrDown" })
	public BaseResponseBody updateUpAndDown(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);
			Integer upOrDown = this.getParameterInteger("upOrDown", null);

			// 获取当前优先级
			ComplainType selectByPrimaryKey = this.complainTypeService.selectByPrimaryKey(id);
			Integer priority = selectByPrimaryKey.getPriority();

			Map<String, Object> map = new HashMap<>();
			map.put("type", upOrDown);
			map.put("priority", priority);
			// 将另外一个升序或降序
			ComplainType complainType = this.complainTypeService.selectByPrioritySort(map);

			ComplainType updateComplainType = new ComplainType();
			updateComplainType.setId(id);
			if (complainType != null) {
				updateComplainType.setPriority(complainType.getPriority());
				complainType.setPriority(priority);
			} else {
				// 等于0是升序 1是降序
				if (upOrDown == 0) {
					updateComplainType.setPriority(priority + 1);
				} else {
					updateComplainType.setPriority(priority - 1);
				}
			}
			if (complainType != null) {
				this.complainTypeService.updatePrority(complainType, updateComplainType);
			} else {
				this.complainTypeService.updateByPrimaryKeySelective(updateComplainType);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.UPDATE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
